package com.example.demo.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.BaseEntity;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import com.example.demo.service.impl.SysUserServiceImpl;
import com.example.demo.utils.CheckSumBuilder;
import com.example.demo.utils.ResponseEntityUtils;
import com.example.demo.utils.http.HttpClient;
import com.example.demo.utils.http.IHttpApiService;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import retrofit2.Response;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private SysUserService sysUserServiceImpl;


    @RequestMapping(value = "/sysLogin", method = RequestMethod.POST)
    public BaseEntity sysLogin(@RequestParam String username, @RequestParam String password) {
        if (StringUtils.isEmpty(username)) {
            return ResponseEntityUtils.getfail("账号不能为空!");
        }
        if (StringUtils.isEmpty(password)) {
            return ResponseEntityUtils.getfail("密码不能为空!");
        }
        SysUser sysUser = sysUserServiceImpl.getOne(new QueryWrapper<SysUser>()
                .lambda().eq(SysUser::getUserName, username).eq(SysUser::getPassword, password));

        if (sysUser == null) {
            return ResponseEntityUtils.getfail("账号或密码错误!");
        }
        sysUser.setPassword(null);
        return ResponseEntityUtils.getsuccess(sysUser);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseEntity login(@RequestParam String username, @RequestParam String password) {
        if (StringUtils.isEmpty(username)) {
            return ResponseEntityUtils.getfail("账号不能为空!");
        }
        if (StringUtils.isEmpty(password)) {
            return ResponseEntityUtils.getfail("密码不能为空!");
        }
        SysUser sysUser = sysUserServiceImpl.getUser(username);
        if (sysUser == null) {
            return ResponseEntityUtils.getfail("账号或密码错误!");
        }
        sysUser.setPassword(null);
        return ResponseEntityUtils.getsuccess(sysUser);
    }


    @RequestMapping(value = "/gettoken", method = RequestMethod.GET)
    public BaseEntity gettone(@RequestParam String uid) {


        HttpClient.getInstance();
        String url = "https://api.netease.im/nimserver/user/getToken.action";

        String appKey = "e68454c2620aff0cc3a084e2a4fe2639";
        String appSecret = "634e9c081e70";
        String nonce = "12345";
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码


        Map<String, String> headmap = new HashMap<>();
        headmap.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        headmap.put("AppKey", appKey);
        headmap.put("Nonce", nonce);
        headmap.put("CurTime", curTime);
        headmap.put("CheckSum", checkSum);


        Map<String, Object> reqMap = new HashMap<>();
//        reqMap.put("uid", "123");
        reqMap.put("uid", Integer.parseInt(uid));
        reqMap.put("channelName", "358013720");

        IHttpApiService service = HttpClient.getInstance().create(IHttpApiService.class);

        String restr = "";
        try {
            Response<ResponseBody> responseBodyResponse = service.postWYYPToken(url, headmap, reqMap).execute();

            JsonObject jsonObject = JsonParser.parseString(responseBodyResponse.body().string()).getAsJsonObject();
//            {
//                "code":200, "token":"028fb1eb32678d8e95ec3b03af05a77662906917"
//            }
            if (jsonObject.get("code").getAsInt() == 200) {
                restr = jsonObject.get("token").getAsString();
            }
            log.error("~~~~~~~~~~~~~~" + responseBodyResponse.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ResponseEntityUtils.getsuccess(restr);
    }

}
